<%@ Page Language="C#"%>
<%@ Import Namespace="SubSonic" %>
<%@ Import Namespace="System.Data" %>

<%foreach(DataProvider p in DataService.Providers){

      TableSchema.Table[] tables = DataService.GetTables(p.Name);
      TableSchema.Table[] views = DataService.GetViews(p.Name);

%>
Namespace <%=p.GeneratedNamespace%>
	#Region "Tables Struct"
	Public Partial Structure Tables
		Dim x As Integer
		<%
			foreach (TableSchema.Table t in tables)
			{
			    if(CodeService.ShouldGenerate(t.Name, p.Name))
			    {
%>
		Public Shared  <%= t.ClassName %> As String = "<%= t.TableName %>"
        <%
			    }
			}
%>
	End Structure
	#End Region

    #Region "View Struct"
    Public Partial Structure Views
		Dim x As Integer
		<%
			foreach (TableSchema.Table v in views)
			{
				if (CodeService.ShouldGenerate(v.Name, p.Name))
				{
%>
		Public Shared <%= v.ClassName %> As String = "<%= v.TableName %>"
        <%
			}
		}
%>
    End Structure
    #End Region

	#Region "Query Factories"
	Public Partial Class DB

		Private Sub New()
		End Sub

        Public Shared Function SelectAllColumnsFrom(Of T As {RecordBase(Of T), New})() As [Select]
			Return SubSonic.Select.AllColumnsFrom(Of T)()
		End Function

		Public Shared Function [Select]() As [Select]
			Return New [Select](DataService.Providers("<%=p.Name%>"))
		End Function

		Public Shared Function [Select](ParamArray ByVal columns As String()) As [Select]
			Return New [Select](DataService.Providers("<%=p.Name%>"), columns)
		End Function

		Public Shared Function [Select](ParamArray ByVal aggregates As Aggregate()) As [Select]
			Return New [Select](DataService.Providers("<%=p.Name%>"), aggregates)
		End Function

		Public Shared Function Update(Of T As {ActiveRecord(Of T), New})() As Update
			Return New SubSonic.Update(New T().GetSchema())
		End Function
		
		Public Shared Function Insert() As Insert
			Dim i As Insert = New Insert()
			i.provider=DataService.Providers("<%=p.Name%>")
			Return i
		End Function

		Public Shared Function Delete() As Delete
			Return New Delete("<%=p.Name%>")
		End Function

		Public Shared Function Query() As InlineQuery
			   Return New InlineQuery("<%=p.Name%>")
		End Function

        <%if (p.TableBaseClass=="RepositoryRecord"){%>
        
        #Region "Repository Compliance"
        
        Public Shared Function [Get](Of T As {RepositoryRecord(Of T), New})(ByVal primaryKeyValue As Object) As T
            Dim item As New T()
            Dim tbl As TableSchema.Table = item.GetSchema()
            Dim pkColumn As String = tbl.PrimaryKey.ColumnName
            Dim rdr As IDataReader = SelectAllColumnsFrom(Of T)().Where(pkColumn).IsEqualTo(primaryKeyValue).ExecuteReader()
           
            If rdr.Read() Then
                item.Load(rdr)
            End If
           
            Return item
        End Function
        
        Public Shared Function [Get](Of T As {RepositoryRecord(Of T), New})(ByVal columnName As String, ByVal columnValue As Object) As T
            Dim item As New T()
            Dim rdr As IDataReader = SelectAllColumnsFrom(Of T)().Where(columnName).IsEqualTo(columnValue).ExecuteReader()
           
            If rdr.Read() Then
                item.Load(rdr)
            End If
           
            Return item
        End Function
        
        Public Shared Sub Delete(Of T As {RepositoryRecord(Of T), New})(ByVal columnName As String, ByVal columnValue As Object)
            Dim item As New T()
            Dim tbl As TableSchema.Table = item.GetSchema()
           
            If tbl.GetColumn("Deleted") IsNot Nothing Then
                'create an update command
                   
                Dim execInt as Integer = New Update(tbl).[Set]("Deleted").EqualTo(True).Where(columnName).IsEqualTo(columnValue).Execute()
        ElseIf tbl.GetColumn("IsDeleted") IsNot Nothing Then
                Dim execInt as Integer = New Update(tbl).[Set]("IsDeleted").EqualTo(True).Where(columnName).IsEqualTo(columnValue).Execute()
            Else
                Dim del As QueryCommand = ActiveHelper(Of T).GetDeleteCommand(columnName, columnValue)
                DataService.ExecuteQuery(del)
            End If
        End Sub
        
        Public Shared Sub Delete(Of T As {RepositoryRecord(Of T), New})(ByVal item As RepositoryRecord(Of T))
            Dim tbl As TableSchema.Table = item.GetSchema()
            Dim pkColumn As String = tbl.PrimaryKey.ColumnName
            Dim pkValue As Object = item.GetColumnValue(pkColumn)
           
            Delete(Of T)(pkColumn, pkValue)
        End Sub
        
        Public Shared Sub Destroy(Of T As {RepositoryRecord(Of T), New})(ByVal item As RepositoryRecord(Of T))
            Dim tbl As TableSchema.Table = item.GetSchema()
            Dim pkColumn As String = tbl.PrimaryKey.ColumnName
            Dim pkValue As Object = item.GetColumnValue(pkColumn)
           
            Dim execInt as Integer = New Delete().From(tbl).Where(pkColumn).IsEqualTo(pkValue).Execute()
        End Sub
        
        Public Shared Sub Destroy(Of T As {RepositoryRecord(Of T), New})(ByVal columnName As String, ByVal value As Object)
            Dim item As New T()
            Dim tbl As TableSchema.Table = item.GetSchema()
            Dim execInt as Integer = New Delete().From(tbl).Where(columnName).IsEqualTo(value).Execute()
        End Sub
        
        Public Shared Function Save(Of T As {RepositoryRecord(Of T), New})(ByVal item As RepositoryRecord(Of T)) As Integer
            Return Save(Of T)(item, "")
        End Function
        
        Public Shared Function Save(Of T As {RepositoryRecord(Of T), New})(ByVal item As RepositoryRecord(Of T), ByVal userName As String) As Integer
            'validation
            Dim result As Integer = 0
            item.ValidateColumnSettings()
            If item.HasErrors() Then
                Dim sb As New StringBuilder()
                Dim errors As List(Of String) = item.GetErrors()
                For Each [error] As String In errors
                    sb.Append([error] + Environment.NewLine)
                Next
               
                Throw New SubSonic.SqlQueryException("There are errors saving this item: " + sb.ToString())
            Else
                If item.IsNew Then
                    result = Insert(Of T)(item, userName)
                Else
                    result = Update(Of T)(item, userName)
                End If
            End If
            item.DirtyColumns.Clear()
            item.MarkOld()
            item.MarkClean()
           
            Return result
        End Function
        
        Public Shared Function Update(Of T As {RepositoryRecord(Of T), New})(ByVal item As RepositoryRecord(Of T)) As Integer
            Return Update(Of T)(item, "")
        End Function
        
        Friend Shared Function Update(Of T As {RepositoryRecord(Of T), New})(ByVal item As RepositoryRecord(Of T), ByVal userName As String) As Integer
            If userName Is Nothing Then
                Throw New ArgumentNullException("userName")
            End If
            Dim result As Integer = 0
           
            Dim cmd As QueryCommand = ActiveHelper(Of T).GetUpdateCommand(item, userName)
            result = DataService.ExecuteQuery(cmd)
           
            Return result
        End Function
        
        Friend Shared Function Insert(Of T As {RepositoryRecord(Of T), New})(ByVal item As RepositoryRecord(Of T)) As Integer
            Return Insert(Of T)(item, "")
        End Function
        
        Private Shared Function Insert(Of T As {RepositoryRecord(Of T), New})(ByVal item As RepositoryRecord(Of T), ByVal userName As String) As Integer
            If userName Is Nothing Then
                Throw New ArgumentNullException("userName")
            End If
            Dim result As Integer = 0
            Dim cmd As QueryCommand = ActiveHelper(Of T).GetInsertCommand(item, userName)
            Dim qResult As Object = DataService.ExecuteScalar(cmd)
           
            If qResult IsNot Nothing Then
                item.SetColumnValue(item.GetSchema().PrimaryKey.ColumnName, qResult)
            End If
           
            Return result
        End Function

        #End Region
        <%}%>
	End Class
	#End Region

End Namespace
<%} %>
 


#Region "Databases"
Public Partial Structure Databases
	Dim x As Integer
	<%foreach (DataProvider p in DataService.Providers) { %>
	Public Shared <%= p.Name %> As String = "<%= p.Name%>"
    <%}%>
End Structure
#End Region
